#!/usr/bin/env python
# encoding: utf-8
"""
comando.py

Created by Martin Schiøth Dyrby on 2012-03-17.
Copyright (c) 2012 __MyCompanyName__. All rights reserved.
"""

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from comando.comando import Comando

class ComandoMainHandler(webapp.RequestHandler):
	def get(self):
		self.doRequest('get')
	def post(self):
		self.doRequest('post')
	def doRequest(self, method):
		comando = Comando()
		comando.init(self.request, self.response)
		service = self.request.get('service')
		if service == '':
			service = self.request.path.split('/')[-1]
		
		result = comando.display(service, method)
		
		if result is not None:
			if result.hasLocation():
				self.response.set_status(302)
				self.response.headers['Location'] = result.location() 
				self.response.clear()
				return

			self.response.out.write(result.response())

def main():
	application = webapp.WSGIApplication([('/.*', ComandoMainHandler)],
		                                         debug=True)
	util.run_wsgi_app(application)


if __name__ == '__main__':
	main()